var WxParse = require('../../wxParse/wxParse.js');
var app = getApp()
// page/component/details/details.js
var app = getApp()
// 定义一个总毫秒数，以一分钟为例。TODO，传入一个时间点，转换成总毫秒数
var total_micro_second = 1534766888000;

/* 毫秒级倒计时 */
function count_down(that) {
  // 渲染倒计时时钟
  that.setData({
    clock: date_format(total_micro_second)
  });

  if (total_micro_second <= 0) {
    that.setData({
      clock: "已经截止"
    });
    // timeout则跳出递归
    return;
  }
  setTimeout(function () {
    // 放在最后--
    total_micro_second -= 10;
    count_down(that);
  }
    , 10)
}

// 时间格式化输出，如03:25:19 86。每10ms都会调用一次
function date_format(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = Math.floor(second / 3600);
  // 分钟位
  var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
  // 秒位
  var sec = fill_zero_prefix((second - hr * 3600 - min * 60));// equal to => var sec = second % 60;
  // 毫秒位，保留2位
  var micro_sec = fill_zero_prefix(Math.floor((micro_second % 1000) / 10));

  return hr + ":" + min + ":" + sec + " " + micro_sec;
}

// 位数不足补零
function fill_zero_prefix(num) {
  return num < 10 ? "0" + num : num
}

Page({
  data: {
    resourceUrl: app.globalData.apiUrl + '/public/uploads/goods/',
    goods: [],
    img: [{ "img": "/../image/1.jpg"}],
    num: 1,
    totalNum: 0,
    hasCarts: false,
    curIndex: 0,
    show: false,
    scaleCart: false,
    clock: ''
  },

  addCount() {
    let num = this.data.num;
    num++;
    this.setData({
      num: num
    })
  },
   reduceCount(){
     let num = this.data.num;
     if(num==1){
       num=1
     }
     else{
     num--;
     }
     this.setData({
       num: num
     })
   },
  addToCart(tap) {
  
  },

  bindTap(e) {
    const index = parseInt(e.currentTarget.dataset.index);
    this.setData({
      curIndex: index
    })
  },
  onLoad(options){
    console.log(options)
    var that = this;
    wx.request({
      url: app.globalData.apiUrl+'/api/goods/groupGoodsInfo',
      data: {id:options.id},
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        console.log(res);
        console.log(res.data.end_time-res.data.start_time)
        total_micro_second =(res.data.end_time-res.data.start_time)*60*24;
        //富文本
        WxParse.wxParse('content', 'html', res.data.content, that, 0);
        that.setData({goods:res.data})
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  
    count_down(this);

  },
  //立即拼团购买
  buy:function(options){
    var that = this;
    var num = this.data.num;
    var openid = app.globalData.openid;
    var id = options.currentTarget.dataset.id;
    console.log(num);
    console.log(options)
    if(openid==null){
      wx.showModal({
        title: '提示',
        content: '亲,您还未登入,请登入后购买',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }else{
      //判断是否有收货地址
      var a = this.selectaddress;
      if(a==''){
        wx.showModal({
          title: '提示',
          content: '亲,您还有有设置收货地址呢',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }///
      //微信支付
      wx.request({
        url: app.globalData.apiUrl + '/api/pay/payfee',//改成你自己的链接
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: { total_fee: that.data.goods.group_price, openid: openid },
        method: 'POST',
        success: function (res) {
          console.log(res.data);
          console.log('调起支付');
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            'success': function (res) {
              //支付成功
              console.log(res);
              wx.showToast({
                title: '支付成功',
                icon: 'success',
                duration: 3000
              });
              console.log('购买中....');
              wx.request({
                url: app.globalData.apiUrl + '/api/pay/groupOrder',
                data: { openid: openid, id: id, num: num },
                method: 'POST',
                dataType: 'json',
                success: function (res) {
                  console.log(res)
                  wx.showModal({
                    title: '提示',
                    content: '成功进入拼团',
                    success: function (res) {
                      if (res.confirm) {
                        console.log('用户点击确定')
                      } else if (res.cancel) {
                        console.log('用户点击取消')
                      }
                    }
                  })
                },
                fail: function (res) { },
                complete: function (res) { },
              })
              
            },
            'fail': function (res) {
              console.log('fail');
            },
            'complete': function (res) {
              console.log('complete');
            }
          });
        },
        fail: function (res) {
          console.log(res.data)
        }
      });
     
    }
    
    

  },
  //查找用户是否有收货地址
  address:function (){
    wx.request({
      url: app.globalData.apiUrl+'/api/user/selectaddress',
      data: {openid:app.globalData.openid},
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        return res.data;
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  }

})